package com.tencent.wxcloudrun.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.tencent.wxcloudrun.config.ApiResponse;
import com.tencent.wxcloudrun.model.BandInfo;
import com.tencent.wxcloudrun.service.BandInfoService;

@RestController
@RequestMapping("/bandinfo")
public class BandInfoController {

  private Gson gson = new GsonBuilder().create();

  @Autowired
  BandInfoService bandInfoService;

  @RequestMapping("/list")
  @ResponseBody
  public ApiResponse list(@RequestBody String str) {
    List<BandInfo> result = bandInfoService.getList();
    return ApiResponse.ok(gson.toJson(result));
  }

  @RequestMapping("/getone")
  @ResponseBody
  public ApiResponse getOne(@RequestBody String str) {
    Map<String, String> input = gson.fromJson(str, new TypeToken<Map<String, String>>() {
    }.getType());
    BandInfo res = bandInfoService.getOne(Integer.valueOf(input.get("id")));
    return ApiResponse.ok(gson.toJson(res));
  }

  @RequestMapping("/vote")
  public ApiResponse vote(@RequestBody String str) {
    BandInfo input = gson.fromJson(str, BandInfo.class);
    bandInfoService.vote(input.getId(), input.getVote());
    return ApiResponse.ok(gson.toJson(bandInfoService.getOne(input.getId())));
  }
}
